using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using FilteredStatistics.View;
using ZoneFiveSoftware.Common.Data.Fitness;
using FilteredStatistics.Common.Controller;

namespace FilteredStatistics.Controller
{
    class Options
    {
        public Options()
        {
            m_StatisticsColumnsManager.PropertyChanged += new PropertyChangedEventHandler(OnStatisticsColumnsManagerPropertyChanged);
        }

        void OnStatisticsColumnsManagerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            TriggerOptionsChangedEvent("StatisticsColumnManager." + e.PropertyName);
        }

        protected void TriggerOptionsChangedEvent(string propertyName)
        {
            if (OptionsChanged != null)
            {
                OptionsChanged(new PropertyChangedEventArgs(propertyName));
            }
        }

        public static Options Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new Options();
                }

                return m_Instance;
            }
        }

        public int ChartSplitDistance
        {
            get { return m_ChartSplitDistance; }
            set
            {
                if (ChartSplitDistance != value)
                {
                    m_ChartSplitDistance = value;
                }
            }
        }

        public int FiltersSplitDistance
        {
            get { return m_FiltersSplitDistance; }
            set
            {
                if (FiltersSplitDistance != value)
                {
                    m_FiltersSplitDistance = value;
                }
            }
        }

        public Color ChartFillColor
        {
            get { return m_ChartFillColor; }
            set
            {
                if(ChartFillColor != value)
                {
                    m_ChartFillColor = value;

                    TriggerOptionsChangedEvent("ChartFillColor");
                }
            }
        }

        public Color ChartLineColor
        {
            get { return m_ChartLineColor; }
            set
            {
                if (ChartLineColor != value)
                {
                    m_ChartLineColor = value;

                    TriggerOptionsChangedEvent("ChartLineColor");
                }
            }
        }

        public Color ChartSelectedColor
        {
            get { return m_ChartSelectedColor; }
            set
            {
                if (ChartSelectedColor != value)
                {
                    m_ChartSelectedColor = value;

                    TriggerOptionsChangedEvent("ChartSelectedColor");
                }
            }
        }

        public Color ChartValidRegionColor
        {
            get { return m_ChartValidRegionColor; }
            set
            {
                if (ChartValidRegionColor != value)
                {
                    m_ChartValidRegionColor = value;

                    TriggerOptionsChangedEvent("ChartValidRegionColor");
                }
            }
        }

        public ActivityDetailsLineChart.XAxisValue ChartXAxis
        {
            get { return m_ChartXAxis; }
            set
            {
                if (ChartXAxis != value)
                {
                    m_ChartXAxis = value;

                    TriggerOptionsChangedEvent("ChartXAxis");
                }
            }
        }

        public Guid ChartYAxis
        {
            get { return m_ChartYAxis; }
            set
            {
                if (ChartYAxis != value)
                {
                    m_ChartYAxis = value;

                    TriggerOptionsChangedEvent("ChartYAxis");
                }
            }
        }

        public int CriteriaTypesColumnWidth
        {
            get { return m_CriteriaTypesColumnWidth; }
            set
            {
                if (CriteriaTypesColumnWidth != value)
                {
                    m_CriteriaTypesColumnWidth = value;

                    TriggerOptionsChangedEvent("FiltersTypeColumnWidth");
                }
            }
        }

        public int NamedZonesColumnWidth
        {
            get { return m_NamedZonesColumnWidth; }
            set
            {
                if (NamedZonesColumnWidth != value)
                {
                    m_NamedZonesColumnWidth = value;

                    TriggerOptionsChangedEvent("ItemsTypeColumnWidth");
                }
            }
        }

        public int OptionsCriteriaTypesColumnWidth
        {
            get { return m_OptionsCriteriaTypesColumnWidth; }
            set
            {
                if (OptionsCriteriaTypesColumnWidth != value)
                {
                    m_OptionsCriteriaTypesColumnWidth = value;

                    TriggerOptionsChangedEvent("OptionsFiltersTypeColumnWidth");
                }
            }
        }

        public int OptionsNamedZonesColumnWidth
        {
            get { return m_OptionsNamedZonesColumnWidth; }
            set
            {
                if (OptionsNamedZonesColumnWidth != value)
                {
                    m_OptionsNamedZonesColumnWidth = value;

                    TriggerOptionsChangedEvent("OptionsItemsTypeColumnWidth");
                }
            }
        }

        public bool DrawColumnsColored
        {
            get { return m_DrawColumnsColored; }
            set
            {
                if (m_DrawColumnsColored != value)
                {
                    m_DrawColumnsColored = value;

                    TriggerOptionsChangedEvent("DrawColumnsColored");
                }
            }
        }

        public bool ShowZonesInChart
        {
            get { return m_ShowZonesInChart; }
            set
            {
                if (m_ShowZonesInChart != value)
                {
                    m_ShowZonesInChart = value;

                    TriggerOptionsChangedEvent("ShowZonesInChart");
                }
            }
        }

        public List<Guid> SelectedExtraCharts
        {
            get { return m_SelectedCharts; }
            set
            {
                m_SelectedCharts.Clear();
                m_SelectedCharts.AddRange(value);

                TriggerOptionsChangedEvent("SelectedCharts");
            }
        }

        public TreeListColumnManager StatisticsColumnsManager
        {
            get { return m_StatisticsColumnsManager; }
        }

        public Point ResultsWindowPosition
        {
            get { return m_ResultsWindowPosition; }
            set
            {
                if (ResultsWindowPosition != value)
                {
                    m_ResultsWindowPosition = value;

                    TriggerOptionsChangedEvent("ResultsWindowPosition");
                }
            }
        }

        public Size ResultsWindowSize
        {
            get { return m_ResultsWindowSize; }
            set
            {
                if (ResultsWindowSize != value)
                {
                    m_ResultsWindowSize = value;

                    TriggerOptionsChangedEvent("ResultsWindowSize");
                }
            }
        }

        public bool ShowTemplatesModifiedWarning
        {
            get { return m_ShowTemplatesModifiedWarning; }
            set
            {
                if (ShowTemplatesModifiedWarning != value)
                {
                    m_ShowTemplatesModifiedWarning = value;

                    TriggerOptionsChangedEvent("ShowTemplatesModifiedWarning");
                }
            }
        }

        public bool ShowSplitsTemplatesWarning
        {
            get { return m_ShowSplitsTemplatesWarning; }
            set
            {
                if (ShowSplitsTemplatesWarning != value)
                {
                    m_ShowSplitsTemplatesWarning = value;

                    TriggerOptionsChangedEvent("ShowSplitsTemplatesWarning");
                }
            }
        }

        public bool ChartOverlayMode
        {
            get { return m_ChartOverlayMode;}
            set
            {
                if(ChartOverlayMode != value)
                {
                    m_ChartOverlayMode = value;

                    TriggerOptionsChangedEvent("ChartOverlayMode");
                }
            }
        }
        public bool UseMapOverlayHighlightAbsolute
        {
            get { return m_UseMapOverlayHighlight; }
        }

        public bool UseMapOverlayHighlight
        {
            get
            {
                return m_UseMapOverlayHighlight &&
                       MapOverlayPlugin.Instance.IsPluginInstalled &&
                       MapOverlayPlugin.Instance.IsHighlightFunctionAvailable &&
                       MapOverlayPlugin.Instance.IsHighlightEnabledGetterAvailable &&
                       MapOverlayPlugin.Instance.IsHighlightEnabled;
            }
            set
            {
                if (UseMapOverlayHighlight != value)
                {
                    m_UseMapOverlayHighlight = value;

                    TriggerOptionsChangedEvent("UseMapOverlayHighlight");
                }
            }
        }

        public delegate void OptionsChangedEventHandler(PropertyChangedEventArgs changedProperty);
        public event OptionsChangedEventHandler OptionsChanged;

        private static Options m_Instance = null;

        private int m_ChartSplitDistance = 200;
        private int m_FiltersSplitDistance = 100;
        private Color m_ChartFillColor = Color.AliceBlue;
        private Color m_ChartLineColor = Color.DodgerBlue;
        private Color m_ChartSelectedColor = Color.SkyBlue;
        private Color m_ChartValidRegionColor = Color.LightBlue;
        private ActivityDetailsLineChart.XAxisValue m_ChartXAxis = ActivityDetailsLineChart.XAxisValue.Time;
        private Guid m_ChartYAxis = ActivityDataTrackController.Instance.GetDefaultProvider().ReferenceId;
        private List<Guid> m_SelectedCharts = new List<Guid>();

        private Point m_ResultsWindowPosition = new Point();
        private Size m_ResultsWindowSize = new Size();

        private int m_CriteriaTypesColumnWidth = 150;
        private int m_NamedZonesColumnWidth = 200;

        private int m_OptionsCriteriaTypesColumnWidth = 150;
        private int m_OptionsNamedZonesColumnWidth = 200;

        private bool m_DrawColumnsColored = true;
        private bool m_ShowZonesInChart = true;

        private bool m_ShowTemplatesModifiedWarning = true;
        private bool m_ShowSplitsTemplatesWarning = true;

        private bool m_UseMapOverlayHighlight = true;

        private bool m_ChartOverlayMode = false;

        private TreeListColumnManager m_StatisticsColumnsManager = new TreeListColumnManager();
    }
}
